package com.service.impl;

import com.constant.Constant;
import com.dao.CourseDao;
import com.dao.CourseTypeDao;
import com.entity.Chapter;
import com.entity.Course;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.service.CourseService;
import com.service.CourseTypeService;
import com.vo.CourseVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * date:2021/4/29
 * description:
 * Author:Stargazer
 */
@Service
public class CourseServiceImpl implements CourseService {
    @Autowired
    private CourseDao courseDao;
    @Autowired
    CourseTypeDao courseTypeDao;

    @Override
    public List<Course> findTop15CoursesList() {
        return courseDao.selectTop15CoursesList();
    }

    @Override
    public List<Course> findThreeCourseByCourseId(Integer course_id) {
        List<Course> courses = courseDao.selectThreeHotCourseByCourseId(course_id);
        return courses;
    }






    @Override
    public PageInfo<CourseVo> findCourseByCourseVo(CourseVo courseVo) {
        String author = courseVo.getAuthor();
        String course_name = courseVo.getCourse_name();
        Integer pageNo = courseVo.getPageNo();
        Integer status = courseVo.getStatus();
        Integer course_type_id = courseVo.getCourse_type_id();
        Date begin_date = courseVo.getBegin_date();
        Date end_date = courseVo.getEnd_date();
        PageHelper.startPage(pageNo, Constant.PAGE_SIZE);
        List<CourseVo> courseVos = courseDao.selectCourseByCourseVo(author, course_name, status, course_type_id, begin_date, end_date);
        return new PageInfo<CourseVo>(courseVos);
    }

    @Transactional
    @Override
    public int modifyCourseById(Integer id, Integer status) {
        int i = courseDao.updateCourseById(id, status);
        return i;
    }
    @Transactional
    @Override
    public int addCourseByCondition(Course course) {
        int i = courseDao.insertCourseByCondition(course);
        return i;
    }

    @Override
    public Course findCourseById(Integer id){
        Course course = courseDao.selectCourseById(id);
        return course;
    }

    @Transactional
    @Override
    public int modifyCourseByCondition(Course course) {

        int i = courseDao.updateCourseByCondition(course);
        return i;
    }
/**
 * ====张,文查询当前类别最火的四个课程
 * 先查询出所有的课程并按照推荐等级和点击量排序
 * 之后
 */

    @Override
    public List<Course> findTop4CourseByGroup() {
        List<Course> courses = courseDao.selectAllCourses();//查询所有的课程并按照推荐等级和点击量排序
        List list = new ArrayList();
        Set ids = new HashSet();//hashset存储的值是无法重复的,重复则新值换旧值
        for (Course course : courses) {
            Integer id = course.getCourse_type_id();
            System.out.println("=========id是========="+id);
            Integer type1id = courseTypeDao.selectParentIdById(id);
            System.out.println("=====父类id是==="+type1id);
            if (type1id!=null){//二级课程
                course.setCourse_type_id(type1id); //此处是为了和前端的筛选对接
                ids.add(type1id);
            }else{//一级课程
                ids.add(id);
            }
        }
        //ids里面存储的是一级类的id
        System.out.println("ids的长度就是有几个类"+ids);
        for (Object id:ids) {//一级类id有几个就循环几次
            List returnList = new ArrayList();
            for (Course course:courses) {
                if(course.getCourse_type_id()==id&& returnList.size()<4){//每一个一级类的都最多添加四个
                    returnList.add(course);
                }
            }
            list.addAll(returnList);
        }
        return list;
    }

    @Override
    public PageInfo findCoursesByCourseTypeId(Integer pageNo, Integer ctid) {
        PageHelper.startPage(pageNo, Constant.PAGE_SIZE_COURSE);
        List<Course> courses = courseDao.selectByCourseTypeId(ctid);
        return new PageInfo(courses);
    }



    @Override
    public List<Chapter> getChapterList(Integer courseId,Integer pageNo) {

        List<Chapter> chapterList = courseDao.getChapterList(courseId,pageNo);
        return chapterList;
    }
//+++++++++++++++++++++++++++++++++++
    @Override
    public PageInfo findByCourseName(Integer pageNo, String courseName) {
        PageHelper.startPage(pageNo, Constant.PAGE_SIZE_COURSE);
        List<Course> courses = courseDao.selectByCourseName(courseName);
        return new PageInfo(courses);
    }

    @Override
    public PageInfo findCoursesByCourseTypeId2(Integer pageNo, Integer ctid) {
        PageHelper.startPage(pageNo, Constant.PAGE_SIZE_COURSE);
        List<Course> courses = courseDao.selectByCourseTypeId2(ctid);
        return new PageInfo(courses);
    }

    @Override
    public List<Course> getCourseList() {
        List<Course> courseList = courseDao.getCourseList();
        return courseList;
    }
//++++++++++++++++    //====张
}
